package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.junit.Test;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 会员档案
 */

@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference
    private MemberService memberService;

    @RequestMapping("/add")
    public Result add(@RequestBody Member member){
        try{
            memberService.add(member);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
        return  new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = memberService.pageQuery(queryPageBean);
        return pageResult;
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Member member){
        try{
            memberService.edit(member);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
        return  new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
    }
    //根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Member member = memberService.findById(id);
            return  new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS,member);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

//        @RequestMapping("/findAll")
//        public Result findAll(){
//            try{
//                List<CheckItem> list = checkItemService.findAll();
//                return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
//            }catch (Exception e){
//                e.printStackTrace();
//                //服务调用失败
//                return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
//            }
//        }
}


